package com.lanou.sfu.songforu.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.sfu.songforu.R;
import com.lanou.sfu.songforu.adapter.SingerContentAdapter;
import com.lanou.sfu.songforu.base.BaseFragment;
import com.lanou.sfu.songforu.constant.Url;
import com.lanou.sfu.songforu.datas.SingerContentEntity;
import com.lanou.sfu.songforu.singleton.VolleySingleton;

import java.util.List;

/**
 * Created by quyan on 15/10/29.
 */
public class SearchContentFragment extends BaseFragment implements View.OnClickListener {
    private ImageView singerImg;
    private ListView songerList;
    private String url;
    private List<SingerContentEntity.DataEntity> data;
    private SingerContentAdapter adapter;
    private VolleySingleton volleySingleton;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.singer_list,null);
        singerImg = (ImageView) view.findViewById(R.id.singer_img);
        songerList = (ListView) view.findViewById(R.id.songer_list);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        volleySingleton = VolleySingleton.getInstance(getActivity());
        singerImg.setOnClickListener(this);
        Bundle bundle = getArguments();
        int id = bundle.getInt("id");
        url = Url.SEARCH_SINGER + id + Url.SEARCH_SINGER_LAST;
        getInternetData();
    }

    //从网络上拉取数据
    private void getInternetData() {
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Gson gson = new Gson();
                SingerContentEntity entity = gson.fromJson(s, SingerContentEntity.class);
                if (entity != null && entity.getData() != null) {
                    data = entity.getData();
                    adapter = new SingerContentAdapter(getActivity(),data);
                    songerList.setAdapter(adapter);
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        getFragmentManager().popBackStack();
    }

}




